package pers.lipeng.greedy_snake;

import javax.swing.*;
import java.awt.*;

/**
 * @author APengG
 * 蛇的身体
 */
public class GreedySnakeBody extends JLabel {
    /**
     * 创建坐标属性便于后面获取
     */
    private int bodyX;
    private int bodyY;

    public GreedySnakeBody(int bodyX,int bodyY){
        this.bodyX = bodyX;
        this.bodyY = bodyY;
        //设置JLabel的背景颜色为绿色
//        this.setBackground(Color.GREEN);
        //设置JLabel的大小为15*15像素
        this.setPreferredSize(new Dimension(15,15));
        //设置身体不透明
//        this.setOpaque(true);
        //默认不为蛇头
        loadIcon(false);
    }

    public GreedySnakeBody(int bodyX,int bodyY,boolean isHead){
        this.bodyX = bodyX;
        this.bodyY = bodyY;
        //设置JLabel的大小为15*15像素
        this.setPreferredSize(new Dimension(15,15));
        loadIcon(isHead);
    }

    private void loadIcon(boolean isHead){
        ImageIcon imageIcon;
        if(isHead){
            imageIcon = new ImageIcon(getClass().getResource("/snake/img/head.png"));
        }else{
            imageIcon = new ImageIcon(getClass().getResource("/snake/img/food.png"));
        }
        //设置图片大小
        Image image = imageIcon.getImage();
        Image scaledInstance = image.getScaledInstance(15, 15, Image.SCALE_DEFAULT);
        imageIcon.setImage(scaledInstance);
        this.setIcon(imageIcon);
    }

    public int getBodyX() {
        return bodyX;
    }

    public void setBodyX(int bodyX) {
        this.bodyX = bodyX;
    }

    public int getBodyY() {
        return bodyY;
    }

    public void setBodyY(int bodyY) {
        this.bodyY = bodyY;
    }
}
